package com.threeidiots.transport.ui.common;

import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * Finds the Bean from populated spring application 
 * context to be used in JSF managed beans.
 * 
 * @author Munish Gogna
 * 
 */
public class SpringBeanFinder {

	public static Object findBean(String beanName) {
		FacesContext context = FacesContext.getCurrentInstance();

		ServletContext servletContext = (ServletContext) context
				.getExternalContext().getContext();
		ApplicationContext appContext = WebApplicationContextUtils
				.getWebApplicationContext(servletContext);

		Object object = appContext.getBean(beanName);

		return object;

	}

}
